package com.it.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.domain.Employee;
import com.it.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/system/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 分页查询员工列表
     */
    @GetMapping("/list")
    public Map<String, Object> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Employee employee) {

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        // 添加查询条件
        if (employee.getName() != null && !employee.getName().isEmpty()) {
            wrapper.like("name", employee.getName());
        }
        if (employee.getPosition() != null && !employee.getPosition().isEmpty()) {
            wrapper.like("position", employee.getPosition());
        }
        if (employee.getEmail() != null && !employee.getEmail().isEmpty()) {
            wrapper.like("email", employee.getEmail());
        }
        if (employee.getPhone() != null && !employee.getPhone().isEmpty()) {
            wrapper.like("phone", employee.getPhone());
        }
        if (employee.getHireDate() != null) {
            wrapper.eq("hireDate", employee.getHireDate());
        }

        IPage<Employee> page = employeeService.page(new Page<>(pageNum, pageSize), wrapper);

        Map<String, Object> result = new HashMap<>();
        result.put("rows", page.getRecords());
        result.put("total", page.getTotal());
        return result;
    }

    /**
     * 获取员工详细信息
     */
    @GetMapping("/{employeeId}")
    public Employee getInfo(@PathVariable Long employeeId) {
        return employeeService.getById(employeeId);
    }

    /**
     * 新增员工
     */
    @PostMapping
    public boolean add(@RequestBody Employee employee) {
        return employeeService.save(employee);
    }

    /**
     * 修改员工信息
     */
    @PutMapping
    public boolean edit(@RequestBody Employee employee) {
        return employeeService.updateById(employee);
    }

    /**
     * 删除员工
     */
    @DeleteMapping("/{employeeIds}")
    public boolean remove(@PathVariable Long[] employeeIds) {
        return employeeService.removeByIds(Arrays.asList(employeeIds));
    }
}